/*
Table 5: Amount of Assistance from Community Members
Table A2: Counterpart of Table 5 with top 1% of observations of the outcome variable winsorised
Table A5: Counterpart of Table 5 with HTE (heterogeneous effects by baseline household asset wealth)
*/

est clear
version 14.0
set more off


// DATA
use "household_level_final.dta", clear
iis hid_base

foreach x in trans_from_tot 	    trans_from_ind	    	trans_from_gvt			trans_from_ngo {
loc labl: var label `x'
	replace `x'=`x'/1000
	label var `x' "`labl' (1000s TSH)"
	
	clonevar `x'_09cons=`x'
	replace `x'_09cons=`x'_09cons/1.196777893 if period==2
	replace `x'_09cons=`x'_09cons/1.388277649 if period==3
	label var `x'_09cons "`labl' in 1000s cons. 2009 TSH"
	
	winsor2 `x'_09cons, cuts(0 99)
	rename `x'_09cons_w `x'_09cons_w1
	label var `x'_09cons_w1 "`x'_09cons with top 1% of obs. winsorized"
	
}

// OUTCOMES AND CONTROL VECTORS (VARIABLES IMBALANCED AT BASELINE)
loc outcomes1	= "trans_from_tot_09cons 	    trans_from_ind_09cons	    	trans_from_gvt_09cons			trans_from_ngo_09cons	"
loc outcomes1a	= "trans_from_ind_09cons	    	trans_from_gvt_09cons			trans_from_ngo_09cons	"
loc outcomes2	= "trans_from_tot_09cons_w1 	    trans_from_ind_09cons_w1	    	trans_from_gvt_09cons_w1			trans_from_ngo_09cons_w1	"
loc imbalancedvars = "improvedfloor_b	dm_trust_comm	dm_food_from_ind	trans_from_tot_09cons	trans_from_ind_09cons	"


// REGRESSIONS

//without HTE, controlling for variables imbalanced at baseline interacted with a time trend

foreach y in `imbalancedvars' {
	clonevar temp= `y'
	replace temp=. if period!=1
	bysort hid_base: egen `y'_b=max(temp)
	loc labb: var label `y'
	gen im_`y'_b_pd2=`y'_b*dmpd2
	label var im_`y'_b_pd2 "BL val of `labb' x pd 2"
	gen im_`y'_b_pd3=`y'_b*dmpd3
	label var im_`y'_b_pd3 "BL val of `labb' x pd 3"
	drop temp 
}

qui foreach y in `outcomes1' `outcomes2' {
	eststo: xtreg 	`y'		assignedtotreat2 	assignedtotreat3	dmpd2	dmpd3	im_*,  cluster(CLID)	fe
}
noi estout *, cells(	b(fmt(%9.0f)star)	se(fmt(%9.0f)) 	) 	stats(N r2,fmt(0 3)) starlevels(* 0.10 ** 0.05 *** 0.01) 

esttab using Table5_and_TableA2.csv, b(2) se(3) r2 starlevels(* 0.1 ** .05 *** .01) obslast replace


//Set up to run with HTE, controlling for variables imbalanced at baseline interacted with a time trend

est clear

foreach k in   		  assignedtotreat2 	assignedtotreat3 dmpd2 dmpd3 	im_improvedfloor_b_b_pd2 im_improvedfloor_b_b_pd3 im_dm_trust_comm_b_pd2 im_dm_trust_comm_b_pd3 im_dm_food_from_ind_b_pd2 im_dm_food_from_ind_b_pd3 im_trans_from_tot_09cons_b_pd2 im_trans_from_tot_09cons_b_pd3 im_trans_from_ind_09cons_b_pd2 im_trans_from_ind_09cons_b_pd3 {
	loc labl: var label `k'
	gen	I`k'		=	dm_poor*`k'	//"I" is for "interact"
	lab var I`k' "`labl' X dummy - poor"
}

loc labb: var label trans_from_tot_09cons
xtreg trans_from_tot_09cons assignedtotreat2 assignedtotreat3 dmpd2 dmpd3 Iassignedtotreat2 Iassignedtotreat3 Idmpd2 Idmpd3	im_* Iim_*, cluster(CLID) fe
forv p = 2/3 {
		**get p-value for interaction term
		loc tt = _b[Iassignedtotreat`p']/_se[Iassignedtotreat`p']
		loc pvaldiff`p'		=  2*ttail(		e(df_r),		abs(`tt')	)	
	
		lincom 	assignedtotreat`p' 	+		Iassignedtotreat`p'		
		loc bI`p'		= 	r(estimate)
		loc seI`p'		=	r(se)
		test 	assignedtotreat`p' +	Iassignedtotreat`p' 	==0
		loc pI`p'		=	r(p)
	}
	
outreg2 using TableA5.xls, bdec(3) pdec(11)  stats(coef se) label replace ctitle("HTE by baseline poverty", "`labb'")	///
		addstat(linear combination of parameters coeff (midline),	`bI2',  std. error(midline), `seI2', ///
		p-value (midline), `pI2',p-value of difference (midline) , `pvaldiff2', ///
		linear combination of parameters coeff (endline),	`bI3',  ///
		std. error(endline), `seI3',  	p-value (endline), `pI3', p-value of difference (endline) , `pvaldiff3')	
 


//HTE by baseline asset wealth poverty

est clear
foreach y in `outcomes1a'	{
	loc labb: var label `y'
	xtreg 	`y'		 assignedtotreat2 	 assignedtotreat3	 dmpd2	 dmpd3	///
					Iassignedtotreat2 	Iassignedtotreat3	Idmpd2	Idmpd3	im_* Iim_*,	cluster(CLID)	fe

	forv p = 2/3 {
		**get p-value for interaction term
		loc tt = _b[Iassignedtotreat`p']/_se[Iassignedtotreat`p']
		loc pvaldiff`p'		=  2*ttail(		e(df_r),		abs(`tt')	)	
	
		lincom 	assignedtotreat`p' 	+		Iassignedtotreat`p'		
		loc bI`p'		= 	r(estimate)
		loc seI`p'		=	r(se)
		test 	assignedtotreat`p' +	Iassignedtotreat`p' 	==0
		loc pI`p'		=	r(p)
	}
	
	outreg2 using TableA5.xls, bdec(3) pdec(11)  stats(coef se) label append ctitle("HTE by baseline poverty", "`labb'")	///
		addstat(linear combination of parameters coeff (midline),	`bI2',  std. error(midline), `seI2', ///
		p-value (midline), `pI2',p-value of difference (midline) , `pvaldiff2', ///
		linear combination of parameters coeff (endline),	`bI3',  ///
		std. error(endline), `seI3',  	p-value (endline), `pI3', p-value of difference (endline) , `pvaldiff3')	

}

est clear


*Baseline means of outcomes for Tables 5 and A2:

summ trans_from_tot_09cons 	    trans_from_ind_09cons	    	trans_from_gvt_09cons			trans_from_ngo_09cons	if period==1

summ trans_from_tot_09cons_w1 	    trans_from_ind_09cons_w1	    	trans_from_gvt_09cons_w1			trans_from_ngo_09cons_w1	if period==1	


*Midline and Endline average amounts of transfers from government for households in treatment villages (for Table 1 footnote)

sum(trans_from_gvt_09cons) if treat_village==1 & period==2
sum(trans_from_gvt_09cons) if treat_village==1 & period==3

exit
